﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Fighter.GameContent
{
    class Boss : Enemy
    {
        protected int intervalleMunition;
        public int IntervalleMunition
        {
            get { return intervalleMunition; }
            set { intervalleMunition = value; }
        }
        protected List<Munition> listMunitions = new List<Munition>();
        private int augmentationAlertX;
        private int augmentationAlertY;
        public List<Munition> ListMunitions
        {
            get { return this.listMunitions; }
        }
        public Rectangle ZoneAlert
        {
            get { return zoneAlert; }
        }
        Rectangle zoneAlert;

        public Boss(Level level, Vector2 position, string spriteSet, GraphicsDeviceManager graphics, int life, Rectangle zoneAlert)
            : base(level, position, spriteSet, graphics, life)
        {
            this.zoneAlert = zoneAlert;
        }
        public Boss(Level level, Vector2 position, string spriteSet, GraphicsDeviceManager graphics, int life, int zoneAlertX, int zoneAlertY)
            : base(level, position, spriteSet, graphics, life)
        {
            this.augmentationAlertX = zoneAlertX;
            this.augmentationAlertY = zoneAlertY;

        }
        /// <summary>
        /// c'est la zone d'attaque du boss
        /// </summary>
        /// <param name="enemy"></param>
        public void updateZoneAlert(Rectangle enemy)
        {
            if ((int)this.direction == -1)
            {
                zoneAlert.Y = enemy.Top - this.augmentationAlertY;
                zoneAlert.Height = enemy.Height + (this.augmentationAlertY * 2);
                zoneAlert.X = enemy.Left - this.augmentationAlertX;
                zoneAlert.Width = enemy.Width + this.augmentationAlertX; 
            }
            else
            {
                zoneAlert.Y = enemy.Top - this.augmentationAlertY;
                zoneAlert.Height = enemy.Height + (this.augmentationAlertY * 2);
                zoneAlert.X = enemy.Left - this.augmentationAlertX;
                zoneAlert.Width = enemy.Width + (this.augmentationAlertX * 2);
            }

        }

        public override void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            this.updateZoneAlert(this.BoundingRectangle);
            bool attacable;
            attacable = isPlayerAttacable(Level.Player.BoundingRectangle);


            if (waitTime > 0)
            {
                waitTime = Math.Max(0.0f, waitTime - (float)gameTime.ElapsedGameTime.TotalSeconds);
                if (waitTime <= 0.0f)
                {
                    // Then turn around.
                    direction = (FaceDirection)(-(int)direction);
                }
            }
            else
            {
                // Calculate tile position based on the side we are walking towards.
                float posX = Position.X + localBounds.Width / 2 * (int)direction;
                int tileX = (int)Math.Floor(posX / Tile.Width) - (int)direction;
                int tileY = (int)Math.Floor(Position.Y / Tile.Height);
                // If we are about to run into a wall or off a cliff, start waiting.
                if (Level.GetCollision(tileX + (int)direction, tileY - 4) == TileCollision.Impassable ||
                    Level.GetCollision(tileX + (int)direction, tileY - 3) == TileCollision.Impassable ||
                    Level.GetCollision(tileX + (int)direction, tileY - 2) == TileCollision.Impassable ||
                    Level.GetCollision(tileX + (int)direction, tileY - 1) == TileCollision.Impassable ||
                    Level.GetCollision(tileX + (int)direction, tileY) == TileCollision.Passable)
                {
                    waitTime = MaxWaitTime;
                }
                else
                {
                    // Move in the current direction.
                    Vector2 velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f);
                    position = position + velocity;
                }
                if (isAgressive)
                {
                    if (attacable == true)
                    {
                        if (intervalleMunition == 0)
                        {
                            Rectangle cible = Level.Player.BoundingRectangle;
                            Vector2 startPositionMunition = this.Position;
                            startPositionMunition.Y -= this.BoundingRectangle.Height / 2;
                            Munition m = new Munition(Level, startPositionMunition, graphics, cible, (int)direction);
                            listMunitions.Add(m);
                            intervalleMunition++;
                        }
                        else
                        {
                            intervalleMunition++;
                            if (intervalleMunition == 50) intervalleMunition = 0;
                        }

                    }
                }
            }

            List<Munition> listMunitionsToRemove = new List<Munition>();
            foreach (Munition munition in listMunitions)
            {
                munition.UpdateForBoss(gameTime);
                if (munition.munitionTouched() != null)
                {
                    listMunitionsToRemove.Add(munition.munitionTouched());
                    Level.Player.OnTouch(this, gameTime);
                }

            }
            foreach (Munition munition in listMunitionsToRemove)
                listMunitions.Remove(munition);

        }

        public Boolean isPlayerAttacable(Rectangle playerRec)
        {

            if (this.zoneAlert.Intersects(playerRec))
            {
                return true;
            }
            else return false;
        }


    }
}
